"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ODBC_1 = __importDefault(require("./ODBC"));
const hasFile_1 = __importDefault(require("../../lib/FSTool/hasFile"));
let sqlite3;
class Sqlite3Promise extends ODBC_1.default {
    rebuildSQL;
    constructor(path, type = 'file', rebuildSQL = '') {
        super(path, type);
        sqlite3 = sqlite3 || require('sqlite3').verbose();
        this.rebuildSQL = rebuildSQL;
    }
    async open() {
        if (this.db) {
            return this.db;
        }
        else if (this.type === 'memory') {
            this.db = new sqlite3.Database(':memory:');
            return this.db;
        }
        else if (this.type === 'file') {
            const isExist = await (0, hasFile_1.default)(this.path);
            this.db = new sqlite3.Database(this.path);
            if (!isExist && this.rebuildSQL) {
                await this.exec(this.rebuildSQL);
            }
            await this.exec('PRAGMA foreign_keys = ON;');
            return this.db;
        }
        else {
            console.log(`创建数据库失败，无法识别的类型${this.type}，仅支持：【file】 or 【memory】`);
            return null;
        }
    }
    ;
    async run(sqlStr, params) {
        let that = this;
        await that.open();
        return new Promise(function (resolve, reject) {
            if (!params)
                params = [];
            that.db.run(sqlStr, params, function (err) {
                if (err) {
                    reject('执行SQL语句失败：' + sqlStr + err.message);
                }
                else {
                    let { lastID, changes } = this;
                    resolve({ lastID, changes });
                }
            });
        });
    }
    ;
    async all(query, params) {
        let that = this;
        await that.open();
        return new Promise(function (resolve, reject) {
            if (!params)
                params = [];
            that.db.all(query, params, function (err, row) {
                if (err) {
                    console.log(err);
                }
                resolve(row || []);
            });
        });
    }
    async exec(sql) {
        let that = this;
        await that.open();
        return new Promise(function (resolve, reject) {
            that.db.exec(sql, function (err, rows) {
                if (err) {
                    reject('Read error: ' + err.message);
                }
                else {
                    resolve(true);
                }
            });
        });
    }
    ;
    async beginTransaction() {
        await this.exec('BEGIN TRANSACTION;;');
    }
    ;
    async commitTransaction() {
        await this.exec('COMMIT TRANSACTION;');
    }
    ;
    async insertData(sql, tileData) {
        let that = this;
        return new Promise(function (resolve) {
            that.db.run(sql, tileData, function (err) {
                let { lastID, changes } = this;
                if (err) {
                    console.log(err);
                }
                resolve({ lastID, changes });
            });
        });
    }
    ;
    insertByParamsArr(sqlStr, objs) {
        return Promise.resolve(false);
    }
    async close() {
        if (this.db && this.db.close) {
            return new Promise((resolve) => {
                this.db.close((err) => {
                    if (err) {
                        resolve(false);
                    }
                    else {
                        resolve(true);
                    }
                });
            });
        }
        else {
            return false;
        }
    }
    ;
    backup(dbPath) {
        return Promise.resolve(false);
    }
    insertByObjs(sqlStr, objs) {
        return Promise.resolve({ changes: null, lastID: '' });
    }
    async get(query, params) {
        const rows = await this.all(query, params);
        return rows && rows.length > 0 ? rows[0] : null;
    }
}
exports.default = Sqlite3Promise;
